package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.IExchangerModel;
import controller.IExchangerController;

public class ExchangerView extends JFrame implements IExchangerView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	String title, fromText, toText, convertButtonText;
	private ConverterPanel sourceCurrencyPanel, targetCurrencyPanel;
	private JButton invertCurrencies;
	private ExchangerMenuBar menu;
	private IExchangerModel model;
	private IExchangerController control;
	private ResourceBundleWrapper resourceBundleWrapper;
	private ClipBoardAccess clipBoardAccess;

	public ExchangerView(IExchangerModel model)
			throws HeadlessException {
		super();
		this.resourceBundleWrapper = new ResourceBundleWrapper();
		this.model = model;
		this.setTitle(this.resourceBundleWrapper.getString("title"));
		this.setIconImage((new ImageIcon("img/converter.gif").getImage()));
		this.sourceCurrencyPanel = new ConverterPanel(this, this.resourceBundleWrapper.getString("fromText"), "", this.model
				.getAllCurrenciesInfos());
		this.targetCurrencyPanel = new ConverterPanel(this, this.resourceBundleWrapper.getString("toText"), "", this.model
				.getAllCurrenciesInfos());
		// configuration du composant JTextField du sourcePanel pour n'accepter
		// que des valeurs numeriques
		this.sourceCurrencyPanel.getTextField().setDocument(
				new DigitLimited(this.sourceCurrencyPanel.getTextField()));
		// ajout de l'ecoute sur clavier du compo JTextField du sourcePanel
		this.sourceCurrencyPanel.getTextField().addKeyListener(
				new KeyAdapter() {
					public void keyReleased(KeyEvent event) {
						String srcCurrencyCode = sourceCurrencyPanel
								.obtainComboBoxSelectedItem();
						String targetCurrencyCode = targetCurrencyPanel
								.obtainComboBoxSelectedItem();
						try {
							double value;
							String sourceCurrencyPanelText = sourceCurrencyPanel
									.obtainTextFieldString();
							if (sourceCurrencyPanelText.equals(""))
								value = 0;
							else
								value = Double.valueOf(sourceCurrencyPanelText);
							control.convertValue(srcCurrencyCode, targetCurrencyCode,
									value);
						} catch (NumberFormatException e) {
						}
					}
				});
		this.sourceCurrencyPanel.getTextField().setText("0.0");
		this.clipBoardAccess = new ClipBoardAccess(this.sourceCurrencyPanel.getTextField());
		// ajout de l'ecoute du changement d'etat dans les JComboBox
		this.sourceCurrencyPanel.getComboBox().addActionListener(
				new Calculate());
		this.targetCurrencyPanel.getComboBox().addActionListener(
				new Calculate());
		this.targetCurrencyPanel.setEditable();
		this.invertCurrencies = new JButton("", new ImageIcon(
				"img/invert.png"));
		this.invertCurrencies.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				control.invertCurrencies(sourceCurrencyPanel
						.obtainComboBoxSelectedItem(), targetCurrencyPanel
						.obtainComboBoxSelectedItem());
			}
		});
		JPanel invertCurrenciesPan = new JPanel(new BorderLayout());
		invertCurrenciesPan.add(this.invertCurrencies, BorderLayout.SOUTH);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.menu = new ExchangerMenuBar(this.model, this, this.resourceBundleWrapper, this.clipBoardAccess);
		this.setJMenuBar(this.menu);
		Container c = this.getContentPane();
		c.setLayout(new FlowLayout());
		c.add(this.sourceCurrencyPanel);
		c.add(invertCurrenciesPan);
		c.add(this.targetCurrencyPanel);
		this.pack();
		this.setVisible(true);
	}
	
	public IExchangerModel getModel() {
		return model;
	}

	public IExchangerController getControl() {
		return control;
	}

	public ResourceBundleWrapper getResourceBundleWrapper() {
		return resourceBundleWrapper;
	}

	public ClipBoardAccess getClipBoardAccess() {
		return clipBoardAccess;
	}

	public void showMessageDialog(String message){
		JOptionPane.showMessageDialog(this.getContentPane(), this.resourceBundleWrapper.getString(message), this.resourceBundleWrapper.getString("info"), JOptionPane.INFORMATION_MESSAGE);
	}
	
	@Override
	public void showWarningMessageDialog(String message) {
		JOptionPane.showMessageDialog(this.getContentPane(), this.resourceBundleWrapper.getString(message), this.resourceBundleWrapper.getString("warning"), JOptionPane.WARNING_MESSAGE);
		
	}

	@Override
	public void showErrorMessageDialog(String message) {
		JOptionPane.showMessageDialog(this.getContentPane(), this.resourceBundleWrapper.getString(message), this.resourceBundleWrapper.getString("error"), JOptionPane.ERROR_MESSAGE);	
	}

	@Override
	public void rateChanged() {
		new Calculate().actionPerformed(null);
	}

	@Override
	public void currencyAdded() {
		this.targetCurrencyPanel.changeComboBoxList(model
				.getAllCurrenciesInfos());
		this.sourceCurrencyPanel.changeComboBoxList(model
				.getAllCurrenciesInfos());
		for(String[] info : model.getAllCurrenciesInfos()){
			//info[2]=code de la monnaie et info[1] son nom
			this.resourceBundleWrapper.addKeyValue(info[2], info[1]);
		}
		repaint();
	}

	@Override
	public void currencyRemoved() {
		this.targetCurrencyPanel.changeComboBoxList(model
				.getAllCurrenciesInfos());
		this.sourceCurrencyPanel.changeComboBoxList(model.getAllCurrenciesInfos());
	}

	@Override
	public void setResult(double result) {
		this.targetCurrencyPanel.changeTextField(Double.toString(result));

	}

	@Override
	public void setControl(IExchangerController control) {
		this.control = control;
		//this.menu.setControl(control);
		this.clipBoardAccess.setControl(control);
	}

	@Override
	public void setSrcCurrency(String srcCurrencyCode) {
		this.sourceCurrencyPanel.selectItem(srcCurrencyCode);
		repaint();
	}

	@Override
	public void setTrgCurrency(String trgCurrencyCode) {
		this.targetCurrencyPanel.selectItem(trgCurrencyCode);
		repaint();
	}

	private class Calculate extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			String srcCodeCurrency = sourceCurrencyPanel.obtainComboBoxSelectedItem();
			String targetCodeCurrency = targetCurrencyPanel
					.obtainComboBoxSelectedItem();
			double value;
			String sourceCurrencyPanelText = sourceCurrencyPanel
					.obtainTextFieldString();
			if (sourceCurrencyPanelText.equals(""))
				value = 0;
			else
				value = Double.valueOf(sourceCurrencyPanelText);
			if(control != null)
				control.convertValue(srcCodeCurrency, targetCodeCurrency, value);
		}
	}

	@Override
	public void refresh() {
		this.resourceBundleWrapper.refreshResourcesBundles();
		repaint();
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		this.sourceCurrencyPanel.setTitle(this.resourceBundleWrapper.getString("fromText"));
		this.targetCurrencyPanel.setTitle(this.resourceBundleWrapper.getString("toText"));
		this.setTitle(this.resourceBundleWrapper.getString("title"));
		new Calculate().actionPerformed(null);
	}

}
